include("util.js");

var karotz_ip="192.168.0.15"
var Colors = ["FF0000","00FF00","0000FF","FFFF00","00FFFF","FF00FF","FF0088","8800FF","FF8888","FFFFFF"]; 
var min=0.25;
var ms=min*60*1000;
var fadeDelay=800;
var iteration=0;

var buttonListener = function(event) {
    if (event == "DOUBLE") {
        exit();
    }
    return true;
}

var debut=function(event) {
	if((event == "CANCELLED") || (event == "TERMINATED")) {
		karotz.tts.start("Plop", "fr", maFonction);
   }
}

var fin=function(event) {
	if((event == "CANCELLED") || (event == "TERMINATED")) {
		karotz.tts.start("Hop hop hop c'est fini.", "fr", endFunction);
   }
}

var endFunction=function(event) {
	if((event == "CANCELLED") || (event == "TERMINATED")) {
		exit();
   }
}

var maFonction = function(event) {
log("Iteration : "+iteration);
if((event == "CANCELLED") || (event == "TERMINATED")) {
	if((iteration*fadeDelay) >= ms)
	{
		var randomnumber = Math.floor(Math.random()*10);
		karotz.led.fade(Colors[randomnumber],fadeDelay,fin);
	}
	else {
		iteration++;
		var randomnumber = Math.floor(Math.random()*10);
		karotz.led.fade(Colors[randomnumber],fadeDelay,maFonction);
		}
   }
}

var onKarotzConnect = function(data) {
    karotz.button.addListener(buttonListener);
    karotz.led.fade('FFFFFF',fadeDelay,debut);

} 

karotz.connectAndStart(karotz_ip, 9123, onKarotzConnect, {});
